package kit.msc.Listener;

import java.awt.Component;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.HashMap;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import kit.msc.gui.SetTimeFrm;
import kit.msc.utility.CardLayoutManager;
import kit.msc.utility.KeyStroker;

public class ShortcutDispatcher {

	/** map containing all global actions */
	private HashMap<KeyStroke, Action> actionMap = new HashMap<KeyStroke, Action>();

	public ShortcutDispatcher() {
		setup();
	}

	/** call this somewhere in your GUI construction */
	private void setup() {

		actionMap.put(KeyStroker.getKeyStroke("VK_DEBUG", KeyEvent.VK_D, InputEvent.CTRL_DOWN_MASK), new AbstractAction("VK_UNNOKN") {
			/**
      *
      */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
				// System.out.println("VK_DEBUG pressed: " + e);
				new SetTimeFrm();
			}
		});

		actionMap.put(KeyStroker.getKeyStroke("VK_DISPOSE", KeyEvent.VK_ESCAPE), new AbstractAction("VK_DISPOSE") {
			/**
       *
       */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
				Component root = SwingUtilities.getRoot((Component) e.getSource());
				if (root instanceof JFrame) {
					((JFrame) root).dispose();
				}
			}
		});

		actionMap.put(KeyStroker.getKeyStroke("VK_CONFIG", KeyEvent.VK_F12), new AbstractAction("VK_CONFIG") {
			/**
       *
       */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {

				Component root = SwingUtilities.getRoot((Component) e.getSource());
				if (root instanceof JFrame) {

					JFrame frame = (JFrame) root;
					if (CardLayoutManager.getActiveCard().equals(CardLayoutManager.ConfigCard)) {
						// switch to last card to dispose the config card
						CardLayoutManager.swichtToLeastCard((JPanel) frame.getContentPane(), frame);
					} else {
						// switch to config card
						CardLayoutManager.switchCard((JPanel) frame.getContentPane(), frame, CardLayoutManager.ConfigCard);
					}

				} else {
					// do I use this? not today!
				}
			}
		});
		// add more actions..

		KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		kfm.addKeyEventDispatcher(new KeyEventDispatcher() {

			@Override
			public boolean dispatchKeyEvent(KeyEvent e) {
				KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(e);
				if (actionMap.containsKey(keyStroke)) {
					final Action a = actionMap.get(keyStroke);
					final ActionEvent ae = new ActionEvent(e.getSource(), e.getID(), null);
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							a.actionPerformed(ae);
						}
					});
					return true;
				}
				return false;
			}
		});
	}
}
